package nl.ajonker.railroad.bl;

import nl.ajonker.railroad.dl.SwitchRail;
import nl.ajonker.railroad.sl.LogProcessor;
import nl.ajonker.railroad.sl.SwitchService;

import java.util.List;

public class MockSwitchService implements SwitchService {
    private LogProcessor log;
    private String hostname;
    private Integer port;

    public MockSwitchService() {
        // default constructor
    }

    public MockSwitchService(LogProcessor log) {
        this.log = log;
    }

    @Override
    public void setLogProcessor(LogProcessor logProcessor) {
        this.log = logProcessor;
    }

    @Override
    public void setAddress(String hostname, Integer port) {
        this.hostname = hostname;
        this.port = port;
    }

    @Override
    public String getHostname() {
        return hostname;
    }

    @Override
    public Integer getPort() {
        return port;
    }

    @Override
    public void performSwitch(SwitchRail rail) {
        if (rail.getSwitchState() == SwitchRail.SwitchState.STRAIGHT)
            performSwitch(rail, SwitchRail.SwitchState.DIVERGING);
        else
            performSwitch(rail, SwitchRail.SwitchState.STRAIGHT);
    }

    @Override
    public void performSwitch(SwitchRail rail, SwitchRail.SwitchState state) {
        if (log != null)
            log.processLog("MockSwitchService performing switch to " + state + " for rail " + rail.getName());
    }

    @Override
    public void initialize(List<SwitchRail> rails) {
        for (SwitchRail rail : rails) {
            performSwitch(rail, rail.getSwitchState());
        }
    }
}
